package au.com.betaversion;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import au.com.betaversion.domain.View;
import au.com.betaversion.repository.ViewRepository;

@Controller
public class ViewController {

	@Autowired
	private ViewRepository viewRepository;
	
	@RequestMapping("/file/stats/{id}")
	public String getStatsForPhoto(@PathVariable("id") String id, Model model) {
		List<View> views = viewRepository.findAllByPhoto(id);
		
		model.addAttribute("views", views);
		
		return "stats";
	}
	
	@RequestMapping("views")
	public String getAllViews(Model model) {
		List<View> views = viewRepository.findAll();
		
		Collections.sort(views, new Comparator<View>() {
			@Override
			public int compare(View o1, View o2) {
				return o2.getDate().compareTo(o1.getDate());
			}
		});
		
		model.addAttribute("views", views);
		
		return "views";
	}
}
